//File Location = C:\Users\janicka\Documents\Personal Documents\CIS 535\Assignment 4\
//Test File = Assignment 4 Input File (SIC).txt
//Test File = Assignment 4 Input File (SIC_XE).txt
package assembler;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.JOptionPane;

public class Assembler {
	public static String [][] sourceFile;
	public static String baseDirectoryInputMessage = "Please enter in the file location of the input file: \nNote: This will also be the location of the output files.";
	public static String inputFileNameMessage = "Please enter in the input file name:";
	public static String SICXEMessage = "Is this a SIC/XE file? (Y/N)";
	public static String completionMessage = "Your output files are located in this directory: \n";
	private static String baseDirectory = "";
	private static String testFileName = "";
	private static String SICXEAnswer = "N";
	public static String intermediateFileName = "IntermediateFile.txt";
	public static String assemblyListingFileName = "AssemblyListing.txt";
	public static String objectCodeFileName = "ObjectCode.txt";
	private static String sourceFileName = null;	
	private static String destinationFileName = null;	
	public static BufferedWriter bufferedWriter;
	public static BufferedWriter bufferedObjectCodeFileWriter;
	public static boolean isSICXE = false;

	public static void main(String[] args) throws Exception{
		baseDirectory = promptUserForInput(baseDirectoryInputMessage);
		testFileName = promptUserForInput(inputFileNameMessage);
		SICXEAnswer = promptUserForInput(SICXEMessage);
		//baseDirectory = "C:\\Users\\janicka\\Documents\\Personal Documents\\CIS 535\\Assignment 4\\";
		//testFileName = "Assignment 4 Input File (SIC).txt";
		//testFileName = "Assignment 4 Input File (SIC_XE).txt";
		//SICXEAnswer = "Y";
		if (SICXEAnswer.equalsIgnoreCase("Y")){
			isSICXE = true;
		}
		sourceFileName =  baseDirectory + testFileName;
		sourceFile = FileReaderImpl.read(sourceFileName);
		PassOne(destinationFileName);		
		sourceFileName = baseDirectory + intermediateFileName;
		sourceFile = FileReaderImpl.read(sourceFileName);
		PassTwo(destinationFileName);
		showMessage(completionMessage, baseDirectory);
	}

	public static String promptUserForInput(String message) throws Exception{
		Object userReponse = JOptionPane.showInputDialog(null, message, "Input",
		JOptionPane.QUESTION_MESSAGE, null, null, null);
		return (String) userReponse;
	}
	
	public static void showMessage(String message, String location) throws Exception{
		JOptionPane.showMessageDialog(null, message + location, "Completed!",
		JOptionPane.PLAIN_MESSAGE);
	}	
	
	public static void createFile(String fileName) throws IOException{
		FileWriter writeFile = null;
		writeFile = new FileWriter(fileName);
		bufferedWriter = new BufferedWriter(writeFile);
	}

	public static void createObjectCodeFile(String fileName) throws IOException{
		FileWriter writeFile = null;
		writeFile = new FileWriter(fileName);
		bufferedObjectCodeFileWriter = new BufferedWriter(writeFile);
	}	
	
	private static void PassOne(String fileName) throws Exception{
		fileName = baseDirectory + intermediateFileName;	
		createFile(fileName);
		for(int i = 0; i < sourceFile.length - 1; i++)
			PassOne.readLine(i);
		PassOne.readLastLine();		
	}	
	private static void PassTwo(String fileName) throws Exception{
		fileName = baseDirectory + assemblyListingFileName;	
		createFile(fileName);
		fileName = baseDirectory + objectCodeFileName;	
		createObjectCodeFile(fileName);
		for(int i = 0; i < sourceFile.length - 2; i++)
			PassTwo.readLine(i);
		if(PassTwo.textRecord.length() != 0) PassTwo.writeBuffer();
		PassTwo.readLastLine();
		PassTwo.outputSymbolTable();
		Assembler.bufferedWriter.close();
		Assembler.bufferedObjectCodeFileWriter.close();
	}
}